package shellstation;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.ResultSet;
import javax.swing.JFrame;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class EditUser extends JFrame implements ActionListener {
    private JFrame frame;
    private JPanel panel;
    private JPanel panel2;
    private JButton submit;
    private JButton search;
    private ResultSet rs;
    private String ut;
    private String u;
    private int uid;
    private int z =1;
    private JLabel ints;
    private JLabel UserType;
    private JLabel Username;
    private JLabel Password;
    private JLabel ConfirmPassword;
    private JLabel SecurityQ;
    private JLabel SecurityA;


    private JComboBox Usertype;
    private String[] listUser = {"Administrator", "Cashier"};
    private JTextField Uname;
    private JPasswordField Pword;
    private JPasswordField CPword;
    private JTextField SQ;
    private JTextField SA;

    public void EditUser(String ut, String n) throws SQLException{
        
        String username  = null;
        String password  = null;
        String cpassword = null;
        String squest    = null;
        String sans      = null;
     
        ints = new JLabel("Leave blank if no changes");
        ints.setBounds(5, 0, 200, 20 );
        ints.setFont(new Font("Hobo Std", Font.BOLD, 10));
           UserController uc = new UserController();
          
            this.rs = uc.getuser(ut,n);
            

        UserType = new JLabel("UserType:");
        UserType.setBounds(60, 30, 200, 20 );
        UserType.setFont(new Font("Hobo Std", Font.BOLD, 15));

        Username = new JLabel("Username:");
        Username.setBounds(60, 80, 200, 20 );
        Username.setFont(new Font("Hobo Std", Font.BOLD, 15));


        Password = new JLabel("Password:");
        Password.setBounds(60, 130, 200, 20 );
        Password.setFont(new Font("Hobo Std", Font.BOLD, 15));

        ConfirmPassword = new JLabel("Confirm Password:");
        ConfirmPassword.setBounds(60, 180, 200, 20 );
        ConfirmPassword.setFont(new Font("Hobo Std", Font.BOLD, 15));

        SecurityQ = new JLabel("Security Question:");
        SecurityQ.setBounds(60, 230, 200, 20 );
        SecurityQ.setFont(new Font("Hobo Std", Font.BOLD, 15));

        SecurityA = new JLabel("Security Asnwer:");
        SecurityA.setBounds(60, 280, 200, 20);
        SecurityA.setFont(new Font("Hobo Std", Font.BOLD, 15));

        submit=new JButton("Submit");
        submit.setBounds(120, 330, 200, 25 );
        submit.setFont(new Font("Hobo Std", Font.BOLD, 15));
        submit.addActionListener(this);

        Usertype = new JComboBox(listUser);
        Usertype.setBounds(200, 30, 200, 20);

        Uname = new JTextField("",20);
        Uname.setBounds(200, 80, 180, 25 );
        Uname.setFont(new Font("Hobo Std", Font.BOLD, 15));

        Pword = new JPasswordField("",16);
        Pword.setBounds(200, 130, 180, 25 );
        Pword.setFont(new Font("Hobo Std", Font.BOLD, 15));


        CPword = new JPasswordField("",16);
        CPword.setBounds(200, 180, 180, 25 );
        CPword.setFont(new Font("Hobo Std", Font.BOLD, 15));

        SQ = new JTextField("",50);
        SQ.setBounds(200, 230, 180, 25 );
        SQ.setFont(new Font("Hobo Std", Font.BOLD, 15));

        SA = new JTextField("",50);
        SA.setBounds(200, 280, 180, 25 );
        SA.setFont(new Font("Hobo Std", Font.BOLD, 15));

        try {
                if (this.rs.next()) {
                    try {  this.uid = this.rs.getInt("userID");
                          username = this.rs.getString("username");
                          password = this.rs.getString("password");
                          cpassword = this.rs.getString("password");
                          squest = this.rs.getString("securityQuestion");
                          sans = this.rs.getString("securityAnswer");
                    } catch (SQLException ex) {
                        Logger.getLogger(LoginPage.class.getName()).log(Level.SEVERE, null, ex);
                    }
                   Uname.setText(username);
                   Pword.setText(password);
                   CPword.setText(cpassword);
                   SQ.setText(squest);
                   SA.setText(sans);


                }

            } catch (SQLException ex) {
                Logger.getLogger(EditUser.class.getName()).log(Level.SEVERE, null, ex);
            }
                          
                          
                          
                          


                    

       
    }


    public void actionPerformed(ActionEvent e){

        if(e.getSource()== submit){
         ResultSet rs1;
         String ut =Usertype.getSelectedItem().toString();
         String n = Uname.getText();
         char[] d = Pword.getPassword();
         char[] s = CPword.getPassword();
         String p = SQ.getText();
         String t = SA.getText();
         String pword = new String(d);
         String cpword = new String(s);


         

        if(n.isEmpty() || p.isEmpty() || t.isEmpty() || pword.isEmpty() || cpword.isEmpty()){
            
       JOptionPane.showMessageDialog(null, "There are blank fields");
        }
            else if(pword == null ? cpword != null : !pword.equals(cpword))
            {
             JOptionPane.showMessageDialog(null, "Password and Confirm Password are not equal");
         }
         else if((pword == cpword) && !n.isEmpty() || !pword.isEmpty() || !cpword.isEmpty() || !p.isEmpty() || !t.isEmpty())
        this.z = JOptionPane.showConfirmDialog(null, "Are you sure you want to save the changes in this user?", "Confirm Add", JOptionPane.YES_NO_OPTION);
        if(z == 0)
        {
            UserController uc = new UserController();
            uc.updateuser(this.uid, ut, n, pword, p, t);
            



            JOptionPane.showMessageDialog(null, "All changes has been saved");
        
        }
        }

        

    }

     public JLabel getints(){
        return ints;
    }

     public void getinfo(String ut, String u){
         this.ut=ut;
         this.u=u;
     }

     public JLabel getUserType(){
        return UserType;
    }

    public JLabel getUserName(){
        return Username;
    }

    public JLabel getPassword(){
        return Password;
    }

    public JLabel getConfirmPassword(){
        return ConfirmPassword;
    }

    public JLabel getSecurityQ(){
        return SecurityQ;
    }

    public JLabel getSecurityA(){
        return SecurityA;
    }

    public JComboBox getuserType(){
        return Usertype;
    }

    public JTextField getUname(){
        return Uname;
    }

    public JPasswordField getPword(){
        return Pword;
    }

    public JPasswordField getCPword(){
        return CPword;
    }

    public JTextField getSQ(){
        return SQ;
    }

    public JTextField getSA(){
        return SA;
    }

    public JButton getsubmit(){
        return submit;
    }
   
}
